/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller;

import com.thienloc.omap.entity.TblConfRate;
import com.thienloc.omap.session.ScratchConfigRateFacade;
import com.thienloc.omap.util.JsfUtil;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author duongnv
 */
@ManagedBean
@RequestScoped
public class ScratchConfigRate extends AbstractCrudBean {

    private int bottom;
    private int up;
    private int level;
    private double rate;
    private List<TblConfRate> configs;
    @EJB
    private ScratchConfigRateFacade configFacade;

    @Override
    protected Logger getLogger() {
        return LoggerFactory.getLogger(ScratchConfigRate.class);
    }

    @Override
    protected void resetToCreateFormAction() throws Exception {
        configs = configFacade.findAll();
    }

    public void delete() throws Exception {
        String level = JsfUtil.getRequestParameter("levelId");
        if (level != null) {
            int levelId = Integer.parseInt(level);
            TblConfRate rate = configFacade.findByLevelId(levelId);
            if (rate != null) {
                configFacade.remove(rate);
            }
        }
    }

    public void fillInput(ActionEvent event) throws Exception {
        String level = JsfUtil.getRequestParameter("levelId");
        if (level != null) {
            int levelId = Integer.parseInt(level);
            TblConfRate rateTbl = configFacade.findByLevelId(levelId);
            setInputTextValue(levelId, rateTbl.getBottomValue(), rateTbl.getUpValue(), rateTbl.getRate());
        }
    }

    public void updateRate(ActionEvent actionEvent) throws Exception {
        TblConfRate rUpdate = configFacade.update(level, bottom, up, rate);
        if (rUpdate != null) {
            configs = configFacade.findAll();
            setInputTextValue(0, 0, 0, 0.0);
            if (rUpdate != null) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Cập nhật dữ liệu", "Thành công."));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cập nhật dữ liệu", "Thất bại."));
            }
        }
    }

    private void setInputTextValue(int levelId, int bottomValue, int upValue, double rateValue) {
        this.level = levelId;
        this.bottom = bottomValue;
        this.up = upValue;
        this.rate = rateValue;
    }

    @Override
    protected void validateForm() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void findSelected(int id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void createSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void editSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void removeSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void prepareViewAction() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void prepareEditAction() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getBottom() {
        return bottom;
    }

    public void setBottom(int bottom) {
        this.bottom = bottom;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    public int getUp() {
        return up;
    }

    public void setUp(int up) {
        this.up = up;
    }

    public List<TblConfRate> getConfigs() {
        return configs;
    }

    public void setConfigs(List<TblConfRate> configs) {
        this.configs = configs;
    }
}
